package com.aoxon.cargo.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.component.MyCallBack;
import com.aoxon.cargo.component.MyProgressDialog;
import com.aoxon.cargo.component.MyTextWatcher;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.service.RevampPasswordService;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.EnvironmentUtil;
import com.aoxon.cargo.util.MD5;
import com.aoxon.cargo.util.ToastUtil;

/**
 * 修改密码界面
 * 
 * @author ZZJ
 * 
 */
public class SupRevampPasswordActivity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {

	private EditText etOldPassword;
	private EditText etNewPassword;
	private EditText etSureNewPassword;

	private ImageView ivReturnButton;
	private ImageView ivCommitButton;

	private TextView tvTitle;
	private LinearLayout llHolder;

	private MyTextWatcher myTextWatcher;
	private MyProgressDialog myProgressDialog;

	private GsonBean gsonBean;

	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);

	private RevampPasswordService revampPasswordService = (RevampPasswordService) MyServiceFactory
			.getInstance(MyServiceFactory.REVAMP_PASSWORD);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setup();
	}


	/**
	 * 自定义方法
	 * ******************************************************************************************************************************************/

	private void setup() {
		setContentView(R.layout.sup_revamp_password);

		etOldPassword = (EditText) findViewById(R.id.etRevampPasswordOld);
		etNewPassword = (EditText) findViewById(R.id.etRevampPasswordNew);
		etSureNewPassword = (EditText) findViewById(R.id.etRevampPasswordSure);

		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		ivCommitButton = (ImageView) findViewById(R.id.ivPublicTitleOther);

		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		llHolder = (LinearLayout) etOldPassword.getParent().getParent();

		myTextWatcher = new MyTextWatcher(new textWhatcherBack());
		myProgressDialog = new MyProgressDialog(this);

		normalDataLoader.setOnloadDataListener(this);

		tvTitle.setText("修改密码");
		ivCommitButton.setImageResource(R.drawable.button_public_commit);
		ivCommitButton.setEnabled(false);

		ivReturnButton.setOnClickListener(myClickListener);
		ivCommitButton.setOnClickListener(myClickListener);
		llHolder.setOnClickListener(myClickListener);

		etOldPassword.addTextChangedListener(myTextWatcher);
		etNewPassword.addTextChangedListener(myTextWatcher);
		etSureNewPassword.addTextChangedListener(myTextWatcher);
	}

	/**
	 * 判断所有输入框是否都有输入
	 * 
	 * @return
	 */
	private void checkAllInput() {
		List<String> strList = getInputList();

		for (int i = 0; i < strList.size(); i++) {
			if (strList.get(i).equals("") || strList.get(i) == null) {
				ivCommitButton.setEnabled(false);
				return;
			}
		}
		ivCommitButton.setEnabled(true);
	}

	/**
	 * 获取所有输入框的文本列表
	 * 
	 * @return
	 */
	private List<String> getInputList() {
		List<String> strList = new ArrayList<String>();

		strList.add(etOldPassword.getText().toString());
		strList.add(etNewPassword.getText().toString());
		strList.add(etSureNewPassword.getText().toString());

		return strList;
	}

	/**
	 * 判断要提交填写的资料格式是否正确
	 * 
	 * @return
	 */
	private boolean checkApplyData() {
		List<String> strList = getInputList();

		for (int i = 0; i < strList.size(); i++) {
			if (strList.get(i).length() < 6 && i != 0) {
				ToastUtil.show(this, "密码不能小于6个字符");
			}
		}
		return true;
	}

	/**
	 * 提交结果的检测
	 */
	private void commitResult() {

		try {
			if (CheckStateUtil.check(gsonBean)) {
				ToastUtil.show(getBaseContext(), "密码修改成功!");

				Supplier supplier = SupIndexShared.getSupplier();
				supplier.setSupPassword(MD5.encrypt(etNewPassword.getText().toString()));
				String strSupplier = DataUtil.gson.toJson(supplier);
				SupIndexShared.saveSupplierAndKey(strSupplier);

				Intent intent = new Intent(getBaseContext(),
						SupSettingActivity.class);
				startActivity(intent);
				this.finish();
			} else {
				ToastUtil.show(getBaseContext(), "帐号密码出错，请重新登录！");
			}

		} catch (Exception e) {
			ToastUtil.show(getBaseContext(), "密码修改失败，请重新提交！");
		}

	}

	/**
	 * 设置各控件的点击监听
	 */
	OnClickListener myClickListener = new OnClickListener() {
		private Intent intent = null;

		@Override
		public void onClick(View v) {

			EnvironmentUtil.closeInputMethod(SupRevampPasswordActivity.this);

			switch (v.getId()) {

			case R.id.ivPublicTitleReturn:
				intent = new Intent(getBaseContext(), SupSettingActivity.class);
				startActivity(intent);
				SupRevampPasswordActivity.this.finish();
				break;

			case R.id.ivPublicTitleOther:
				if (checkApplyData()) {
					normalDataLoader.load();
				}
				break;

			default:
				break;
			}
		}
	};

	/**
	 * 文本监听的回调
	 */
	class textWhatcherBack implements MyCallBack {

		@Override
		public void callback(String choose) {
			checkAllInput();
		}

	}

	/**
	 * 系统按键的设置
	 * *****************************************************************************************************************************************/

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(getBaseContext(),
					SupSettingActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		myProgressDialog.show("提示", "提交中，请稍候...");
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		List<String> strList = getInputList();
		gsonBean = revampPasswordService
				.execute(strList.get(0), strList.get(1));
		msg.what = MyStateUtil.REVAMP_RESULT;
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		myProgressDialog.cancle();
		switch (state) {
		case MyStateUtil.REVAMP_RESULT:
			commitResult();
			break;

		case MyStateUtil.NOT_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "无网络连接");
			break;
		case MyStateUtil.ERROR_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "网络连接异常");
			break;
		default:
			break;
		}
	}

}
